package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.dao.ProductItemDao;
import com.wing.product.model.entity.ProductItem;
import com.wing.product.model.request.ProductItemForm;
import com.wing.product.model.response.ProductItemVO;
import com.wing.res.model.request.TestItemData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 产品类型检测项-ServiceImpl
 *
 * @author xiayang
 */
@Service
@Slf4j
public class ProductItemServiceImpl extends ServiceImpl<ProductItemDao, ProductItem> implements ProductItemService {

    @Override
    @Transactional
    public int add(ProductItemForm productItemForm) {
        //校验
        ProductItem productItem = new ProductItem();
        CopyOptions copyOptions = CopyOptions.create().setEditable(ProductItem.class).setIgnoreError(true).setIgnoreNullValue(true);
        BeanUtil.copyProperties(productItemForm, productItem, copyOptions);
        return baseMapper.insert(productItem);
    }

    @Override
    public List<ProductItemVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductItemVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductItemVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductItemVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public int update(ProductItemForm productItemForm) {
        ProductItem productItem = new ProductItem();
        BeanUtils.copyProperties(productItemForm, productItem);
        return baseMapper.updateById(productItem);
    }

    /**
     * 根据产品类别查询出所有的关联的检测项
     * @param productType
     * @return
     */
    @Override
    public List<TestItemData> listByType(Long productType) {
        return baseMapper.listByType(productType);
    }

    /**
     * 查询检测项目
     * @param id 工序id
     * @return
     */
    @Override
    public List<ProductItem> listItem(Long id) {
        return baseMapper.listItem(id);
    }
}
